﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace community
{
    public partial class deathcheck : Form
    {
        public static string JMBH;
        string sql = "select a.inhabitantcode_chr as 居民编号,a.residentname_chr as 居民姓名,a.communityname_chr as 所属社区,a.sex_chr as 居民性别,a.birth_dat as 出生日期,b.DIEADDRTIME_DAT  as 死亡日期,b.age as 居民年龄 from T_CS_INHABITANTRECORD_NEW as a,T_CS_INHABITANTDIEPROVE as b  where a.inhabitantcode_chr=b.inhabitantcode_chr and b.DIEADDRTIME_DAT!='' ";
        public deathcheck()
        {
            InitializeComponent();
        }

        private void deathcheck_Load(object sender, EventArgs e)
        {
            check(sql);
        }
        private void check(string str)
        {
            SqlConnection  cn = new SqlConnection(SQL.conStr);
            cn.Open();
            SqlDataAdapter dt = new SqlDataAdapter(str, cn);
            DataSet ds = new DataSet();
            dt.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0];
            cn.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                textBox1.Enabled = true;
                textBox1.Focus();
            }
            else 
            {
                textBox1.Enabled = false;
                textBox1.Text = "";
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
            {
                dateTimePicker1.Enabled = true;
                dateTimePicker2.Enabled = true;
            }
            else
            {
                dateTimePicker1.Enabled = false;
                dateTimePicker2.Enabled = false;

            }
        }
        //"select a.inhabitantcode_chr,a.residentname_chr,a.communityname_chr,a.sex_chr,a.birth_dat,b.DIEADDRTIME_DAT,b.age 
        //from T_CS_INHABITANTRECORD_NEW as a,T_CS_INHABITANTDIEPROVE as b  where b.DIEADDRTIME_DAT!='' and  
        //a.communityname_chr='"+textbox1.text+"' and b.DIEADDRTIME_DAT between '"+conv
        private void button1_Click(object sender, EventArgs e)
        {
            sql = "select a.inhabitantcode_chr as 居民编号,a.residentname_chr as 居民姓名,a.communityname_chr as 所属社区,a.sex_chr as 居民性别,a.birth_dat as 出生日期,b.DIEADDRTIME_DAT  as 死亡日期,b.age as 居民年龄 from T_CS_INHABITANTRECORD_NEW as a,T_CS_INHABITANTDIEPROVE as b  where a.inhabitantcode_chr=b.inhabitantcode_chr and b.DIEADDRTIME_DAT!='' ";
            if ((checkBox1.Checked == true) &&  (textBox1.Text == ""))
            {
                MessageBox.Show("请输入检索的社区", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if ((checkBox2.Checked == true) &&  (dateTimePicker1.Text == "") ||  (dateTimePicker2.Text == ""))
            {
                MessageBox.Show("请输入检索的死亡时间段", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            //sql += "select a.inhabitantcode_chr,a.residentname_chr,a.communityname_chr,a.sex_chr,a.birth_dat,b.DIEADDRTIME_DAT,b.age from T_CS_INHABITANTRECORD_NEW as a,T_CS_INHABITANTDIEPROVE as b  where b.DIEADDRTIME_DAT!='' ";
            if (checkBox1.Checked==true && textBox1.Text != "")
                sql += " and a.communityname_chr=' "+textBox1.Text.Trim()+"'";
            if (checkBox2.Checked == true && dateTimePicker1.Text != "" && dateTimePicker2.Text != "")
                sql += "and (b.DIEADDRTIME_DAT between '"+Convert.ToDateTime(dateTimePicker1.Text)+"' and '"+Convert.ToDateTime(dateTimePicker2.Text)+"')";
            check(sql);
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            dataGridView2.Visible = true;
            SqlConnection  cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string sqlStr = "select COMMUNITYCODE_CHR as 社区编号,COMMUNITYNAME_VCHR as 社区名称  from t_cs_community  where  CURRENTCS_INT=1";
            SqlDataAdapter dt = new SqlDataAdapter(sqlStr, cn);
            DataSet ds = new DataSet();
            dt.Fill(ds);
            this.dataGridView2.DataSource = ds.Tables[0];
            dataGridView2.ColumnHeadersHeight = 39;
            dataGridView2.Columns[0].Width = 150;
            dataGridView2.Columns[1].Width = 150;
            cn.Close();
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                textBox1.Text = dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString();
                if (textBox1.Text != null)
                    dataGridView2.Visible = false;
            }
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            //if (e.RowIndex >= 0)
            //{
            //    textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            //}
            //this.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                JMBH = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            }
            this.Close();
        }
    }
}
